package com.my.ph.controller;

import com.my.ph.dto.PublishHelperParam;
import com.my.ph.entity.publish.PublishHelperInfo;
import com.my.ph.enums.MediaTypeEnum;
import com.my.ph.entity.media.TVMovie;
import com.my.ph.entity.media.TVMovieDetail;
import com.my.ph.service.MediaService;
import com.my.ph.util.R;
import com.my.ph.util.TitleValue;
import com.my.ph.util.TorrentUtil;
import com.my.ph.util.UserJsonConfigUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/media")
@RequiredArgsConstructor
public class MediaController {

    private final MediaService mediaService;

    @GetMapping("/search")
    public R<List<TVMovie>> search(String keyword) {
        return R.ok(mediaService.search(keyword));
    }

    @GetMapping("/detail")
    public R<TVMovieDetail> detail(long id, String mediaType) {
        return R.ok(mediaService.getDetail(id, MediaTypeEnum.getByValue(mediaType)));
    }

    @GetMapping("/type")
    public R<List<TitleValue>> type() {
        return R.ok(Arrays.stream(MediaTypeEnum.values()).map(e -> new TitleValue(e.getDesc(), e.getValue())).toList());
    }

    @PostMapping("/publishHelperInfo")
    public R<PublishHelperInfo> getPublishHelperInfo(@RequestBody PublishHelperParam param) {
        return mediaService.getPublishHelperInfo(param);
    }

    @PostMapping("/confirmPublishInfo")
    public R<PublishHelperInfo> confirmPublishInfo(@RequestBody PublishHelperInfo info) {
        return mediaService.confirmPublishInfo(info);
    }

    @PostMapping("/autoFeedUrl/{id}")
    public R<?> autoFeedUrl(@PathVariable Long id) {
        return TorrentUtil.getTorrentInfo(id);
    }

    @GetMapping("/source")
    public R<List<TitleValue>> source() {
        return R.ok(UserJsonConfigUtil.getInstance().getSource().stream().map(e -> new TitleValue(e, e)).toList());
    }

    @GetMapping("/team")
    public R<List<TitleValue>> team() {
        return R.ok(UserJsonConfigUtil.getInstance().getTeam().stream().map(e -> new TitleValue(e, e)).toList());
    }

}
